
// 大华摄像头
const CONFIG = {
  ip: "192.168.2.108",
  port: "80",
  name: "admin",
  pswd: "dk16888888",
  WndIndex: "0",
};

(() => {
  //  登录
  function onLogin(callback) {
    let ip = CONFIG.ip;
    let port = CONFIG.port;
    let name = CONFIG.name;
    let pswd = CONFIG.pswd;
    // debugger;
    let target = ip + ":" + port;
    setIP(target);
    RPC.login(name, pswd, false)
      .then((res) => {
        // console.log("登录设备+++", res);
        setCookie("DWebClientSessionID", "", -1);
        setCookie("DhWebClientSessionID", "", -1);
        callback && callback();
        /**
         * RPC.keepAlive 保活
         */
        RPC.keepAlive(300, 60000, _getSession(), target, CONFIG.WndIndex);
        // this.afterLogin();
        onRealPreview(ip, port, name, pswd); //登录后拉流
      })
      .catch((err) => {
        console.log("err===", err);
      });
  }

  onLogin();
})();

// 登录成功后 拉流
function onRealPreview(ip, port, name, pswd) {
  var h5_video = document.getElementById("h5_video");
  var h5_canvas = document.getElementById("h5_canvas");
  var playerInstance = [];
  if (playerInstance[CONFIG.WndIndex]) {
    playerInstance[CONFIG.WndIndex].stop();
    playerInstance[CONFIG.WndIndex].close();
    playerInstance[CONFIG.WndIndex] = null;
    // playerInstance[0].setAudioVolume(0.8);
    let dom = h5_canvas;
    if (dom.style.display === "none") {
      dom = h5_video;
    }
    dom.style.display = "none";
  }
  var player = null;
  let curChannel = 0 + 1; //无插件通道号从1开始
  let stream = 0 - 0;
  let options = {
    wsURL: "ws://" + ip + ":" + port + "/rtspoverwebsocket",
    rtspURL:
      "rtsp://" +
      ip +
      ":" +
      port +
      "/cam/realmonitor?channel=" +
      curChannel +
      "&subtype=" +
      stream +
      "&proto=Private3",
    username: name,
    password: pswd,
    lessRateCanvas: true,
  };
    console.log("options", options);
  var player = new window.PlayerControl(options);
  // console.log("player", player);

  // 分辨率改变
  player.on("MSEResolutionChanged", function (e) {
    console.log("分辨率改变：", e);
  });

  // 开始播放
  player.on("PlayStart", function (e) {
    console.log("开始播放：", e);
    playerInstance[CONFIG.WndIndex] = player;
    var dom = document.querySelector(".title");
    dom.style.display = "none";
  });

  // 开始解码
  player.on("DecodeStart", function (e) {
    // console.log("开始解码：", e);
    if (e.decodeMode === "video") {
      h5_canvas.style.display = "none";
      h5_video.style.display = "";
    } else {
      h5_video.style.display = "none";
      h5_canvas.style.display = "";
    }
  });

  player.on("GetFrameRate", function (e) {
    // console.log("视频帧率: " + e);
  });

  // 视频编码模式改变
  player.on("FrameTypeChange", function (e) {
    console.log("视频编码模式改变: " + e);
  });

  // 播放发生错误
  player.on("Error", function (e) {
    console.log("播放发生错误: " + JSON.stringify(e));
  });
  // 文件加载完成
  player.on("WorkerReady", function () {
    // console.log("开始拉流");
    player.connect();
  });
  player.init(h5_canvas, h5_video); //初始化播放器
}
